package com.jcvision.mms.util;

/**
 * 翻页信息bean
 *
 * @author Liu BC
 */
public class PageBean {
    // ------------------------------ FIELDS ------------------------------

    private int currentPage = 0;	// 当前页
    private int totalPages = 0;		// 总页数
    private int totalRows = 0;		// 总行数
    private int rowsPPage = 17;		// 每页显示条数
    private int startRowNum;		// 开始行数
    @SuppressWarnings("unused")
	private int offset = 0;			//偏移量
    @SuppressWarnings("unused")
    private boolean hasPageUp = false;	// 是否有上页
    @SuppressWarnings("unused")
    private boolean hasPageDown = false;// 是否有下页
    private Integer[] pageList = null;

    public PageBean(int currentPage, int totalPages) {
        this.setCurrentPage(currentPage);
        this.setTotalRows(totalRows);
    }

    // -------------------------- STATIC METHODS --------------------------

    /**
     * 封装oracle的分页语句
     *
     * @param sql   无分页信息的查询语句
     * @param start 起始行数（包含本行）
     * @param end   结束行数（包含本行）
     */
    public static String getPageSqlForOracle(String sql, int start, int end) {
        StringBuffer buff = new StringBuffer();
        buff.append("SELECT * FROM (SELECT a.*, ROWNUM rn FROM (").append(sql)
                .append(") a WHERE ROWNUM <= ").append(end).append(
                ") WHERE rn >= ").append(start);
        return buff.toString();
    }

    /**
     * 封装mysql的分页语句
     *
     * @param sql   无分页信息的查询语句
     * @param start 起始行数（包含本行）
     */
    public String getPageSqlForMysql(String sql, int start) {
        StringBuffer buff = new StringBuffer(sql);
        buff.append(" limit ").append(start - 1).append(",").append(rowsPPage);
        return buff.toString();
    }

    // --------------------------- CONSTRUCTORS ---------------------------

    public PageBean() {
    }

    public PageBean(int rowsPerPage) {
        setRowsPPage(rowsPerPage);
    }

    // --------------------- GETTER / SETTER METHODS ---------------------

    /**
     * @return 返回当前所在的页数
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * 设置需要的页(<font color="red">前台需设置,默认第一页</font>)
     *
     * @param currentPage
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public Integer[] getPageList() {
        return pageList;
    }

    public void setPageList(Integer[] pageList) {
        this.pageList = pageList;
    }

    /**
     * @return 返回每页显示多少行记录(默认:20)
     */
    public int getRowsPPage() {
        return rowsPPage;
    }

    /**
     * 设置每页显示的行数,不设置默认为20
     *
     * @param rowsPPage
     */
    public void setRowsPPage(int rowsPPage) {
        this.rowsPPage = rowsPPage;
    }

    /**
     * @return 返回从数据库开始取行数
     */
    public int getStartRowNum() {
        return this.startRowNum;
    }

    /**
     * @return 返回总页数
     */
    public int getTotalPages() {
        return totalPages;
    }

    /**
     * 返回所有记录总行数
     *
     * @return
     */
    public int getTotalRows() {
        return totalRows;
    }

    // -------------------------- OTHER METHODS --------------------------

    /**
     * @return 返回是否有下一页(true:有;false:没有)
     */
    public boolean isHasPageDown() {
        return currentPage < totalPages;
    }

    /**
     * @return 返回是否有上一页(true:有;false:没有)
     */
    public boolean isHasPageUp() {
        return currentPage > 1;
    }

    /**
     * 设置总行数(<font color='red'>前台不要调用此方法</font>)
     *
     * @param totalRows
     */
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
        if (0 == totalRows % this.rowsPPage) {
            this.setTotalPages(totalRows / this.rowsPPage);
        } else {
            this.setTotalPages(totalRows / this.rowsPPage + 1);
        }
    }

    /**
     * 设置总页数
     *
     * @param totalPages
     */
    private void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
        if (this.currentPage <= this.totalPages && this.totalPages > 0) {
            if (this.currentPage == 0) {
                this.currentPage = 1;
            }
        } else {
            if (this.totalPages > 0) {
                this.currentPage = 1;
            } else {
                this.currentPage = 0;
            }
        }
        if (this.currentPage > 1) {
            this.hasPageUp = true;
        } else {
            this.hasPageUp = false;
        }
        if (this.currentPage < this.totalPages) {
            this.hasPageDown = true;
        } else {
            this.hasPageDown = false;
        }
        /* 加入通过设置当前页改变开始行数,适用于动态加载 */
        if (this.currentPage > 0) {
            this.startRowNum = (this.currentPage - 1) * this.rowsPPage;
        } else {
            this.startRowNum = this.currentPage * this.rowsPPage;
        }
        this.pageList = new Integer[this.totalPages];
        for (int i = 0; i < this.totalPages; i++) {
            this.pageList[i] = i + 1;
        }
    }

    public int getOffset() {
        return (currentPage - 1) * rowsPPage;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }
}
